<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form
      ref="userForm"
      label-width="220px"
      :rules="rules"
      :model="userInfo"
    >
      <!--手机 -->

      <el-form-item label="手机">
        <el-input v-model="userInfo.mobile" style="width:220px" disabled />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <!-- <img :src="userInfo.staffPhoto"> -->
        <UploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hSave">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getUserDetail, updateUserDetail } from '@/api/user'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''

      },
      id: this.$route.params.id,
      rules: {
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.loadUserDetail()
  },
  methods: {
    async loadUserDetail() {
      try {
        const res = await getUserDetail(this.id)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        // this.$message.error(res.message)
        this.userInfo = res.data
      } catch (error) {
        console.log(error)
      }
    },
    hSave() {
      this.$refs.userForm.validate(async valid => {
        if (!valid) return
        // console.log('校验成功')
        try {
          const res = await updateUserDetail(this.userInfo)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          // this.loadUserDetail()
          this.$router.back()
        } catch (error) {
          console.log(error)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
